﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using Nemerle.ComputationExpressions;
using Nemerle.ComputationExpressions.Async;

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using Microsoft.Cci;

namespace MetadataReader.CCI
{
  public partial class CciMetadataProvider
  {
    class ReaderHost : PeReader.DefaultHost
    {
      public this(provider : CciMetadataProvider)
      {        
        Provider = provider;
        SearchInGAC = true;
        LibPaths.Add(Path.GetDirectoryName(typeof((int->int)).Assembly.Location));
      }

      Provider : CciMetadataProvider { get; }

      //public override LoadUnitFrom(location : string) : Microsoft.Cci.IUnit
      //{
      //	def res = base.LoadUnitFrom(location);
      //  match (res)
      //  {
      //    | a is IAssembly when a != Dummy.Assembly : object => 
      //      lock (GlobalLock.LockingObject)
      //        _ = LoadUnit(a.AssemblyIdentity)
      //    | _                                                => ()
      //  }
      //	res
      //}

      // TODO: more optimal GAC search!
      //public override ProbeAssemblyReference(referringUnit : IUnit, referencedAssembly : AssemblyIdentity) : AssemblyIdentity
      //{
      //  def loc = base.ProbeAssemblyReference(referringUnit, referencedAssembly);
      //  loc
      //}

      public override LoadAssembly(assemblyIdentity : AssemblyIdentity) : IAssembly
      {
        if(assemblyIdentity.Location == "unknown://location")
          Dummy.Assembly
        else
          LoadUnitFrom(assemblyIdentity.Location) :> IAssembly
      }
    }
  }
}
